package com.uchedao.seller.widget.loadstate.base;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchedao.seller.R;


/**
 * 视图工厂 <br>
 */
public class LoadstatueViewFactory {

    /**
     * 添加视图
     *
     * @param view
     */
    private static void addView(View view, View contentView) {
        View parentView = (View) contentView.getParent();
        if (parentView != null) {

            if (parentView instanceof RelativeLayout) {
                RelativeLayout parent = (RelativeLayout) parentView;
                parent.addView(view);
                RelativeLayout.LayoutParams l = new RelativeLayout.LayoutParams(
                        view.getLayoutParams());
                l.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                l.width = RelativeLayout.LayoutParams.MATCH_PARENT;
                l.height = RelativeLayout.LayoutParams.MATCH_PARENT;
                view.setLayoutParams(l);
            } else if (parentView instanceof LinearLayout) {
                LinearLayout parent = (LinearLayout) parentView;
                parent.addView(view);
                LinearLayout.LayoutParams l = new LinearLayout.LayoutParams(
                        view.getLayoutParams());
                l.width = LinearLayout.LayoutParams.MATCH_PARENT;
                l.height = LinearLayout.LayoutParams.MATCH_PARENT;
                l.gravity = Gravity.CENTER;
                view.setLayoutParams(l);
            } else {
                throw new IllegalArgumentException(
                        "ParentView must be a RelativeLayout or LinearLayout!!!");
            }
        }
    }


    /**
     * 获取加载空视图
     *
     * @param context
     * @return
     */
    public static View getLoadEmpty(Context context, View contentView,
                                    View.OnClickListener ocl) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.load_custom_error, null);
        TextView textView = (TextView) view
                .findViewById(R.id.tv_msg_error);
        textView.setOnClickListener(ocl);
        addView(view, contentView);
        return view;
    }

    /**
     * 获取加载空视图(自定义)
     *
     * @param context
     * @return
     */
    public static View getLoadEmpty(Context context, String msg, View contentView,
                                    View.OnClickListener ocl) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.load_custom_error, null);
        TextView textView = (TextView) view
                .findViewById(R.id.tv_msg_error);
        textView.setText(msg);
        textView.setOnClickListener(ocl);
        addView(view, contentView);
        return view;
    }


    /**
     * 获取加载中视图
     *
     * @param context
     * @return
     */
    public static View getLoadingView(Context context, View contentView, String title) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.load_loading_view, null);
        if (!TextUtils.isEmpty(title)) {
            ((TextView) view.findViewById(R.id.load_loading_title_tv)).setText(title);
        }
        view.setVisibility(View.GONE);
        addView(view, contentView);
        return view;
    }

    public static View getLoadingView(Context context, View contentView) {
        return getLoadingView(context, contentView, "");
    }


    /**
     * 获取加载失败视图
     *
     * @param context
     * @return
     */
    public static View getLoadingFailed(Context context, View contentView,
                                        View.OnClickListener ocl) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.load_net_error, null);
        Button btn = (Button) view
                .findViewById(R.id.btn_update);
        btn.setOnClickListener(ocl);
        view.setVisibility(View.GONE);
        addView(view, contentView);
        return view;
    }

}
